Fixing issues 3, 4, 5, 6 and code cleanup

This PR has fixes for issues #3, #4, #5, #6 as well as some general code cleanup.