Restructured code a bit