bug fixes and logic cleanup.
This is version 0.5