stripped unused code & refactored a bit