Reworking code a bit for easier understanding