fixed a few errors and cleaned up code