bug fixes and cleaning up code a little