Restructored some code