refactored to make debugging easier