Refactored the current code a bit