code refracroting, optimisations and bug fixing