Re-arranged some code