refactored, improved readability, removed some console logs