bug fixing, optimisations + code refractoring