Bug fixes and cleaning up code