Remerged manually, using omar's correct up to date code