much cleaner code, also fixed the bug in specifying the correct cube length