externalized magic numbers and refactored a bit