refactored code, did some tweaks