Reorganised some code