reorganizing and cleaning up code