refactored, updated and tested whole module