Simplified bit operations, cleaned up and added some case-switches

I hope I haven't broken anything. Let me know if you don't like anything.