cleaned up code and fixed some issues