refactored & fixed