Fixing off-by-one error and logging improvements