Bug fixes, cleanup, expanded test code