Enhancement: Tested and rewrote/refactored the code