Fixed some boundary checks