fixed some minor bugs and cleaned up the code