fixing a small bug and finalizing the code