refactored the whole code and made room for improvement in the future