I bet I can think of a way to reduce the PR by 70 lines of code... ;)