cleaned the code and fixed a small bug