broken version after trying lots of bug fixing