reduced code size and refactored a bit