refactored due to @laughedelic suggestions #2