General bugfixes

Bugfixes/improvements are in commit messages.
As we discussed, let's make these constants or factor them out into a hash.