Reorgonised all code to work faster.