Fixed a bug caused by using floor()+1 rather than ceiling()