rearranged & refactored drastically