another refactoring round, still need to get rid of most magic numbers