cleaned up code and fixed minor bugs