Cleans the code a bit, compacts several lines

Some of the code is duplicated. There's a lot that can be simplified. This is just one example.