Code seems to work in all cases now