there must be a more readable way to write this, even better if magic numbers aren't used