Significantly refactored the code!!!