Refactored code a little

It is a bit shorter and cleaner now. I checked it - works just fine.