fixed some bugs n refactored the code