Restructured the code a bit