refactored to make everything slightly more logical