Optimizations, a bug, and visibility functions

Hello!

I hope I don't create a mess. I tried to keep everything capsulated in separate commits. Please review the commits.