[modified] Refactored the code a little bit more