adjusted speed and cleaned up code