Bug fix: if statment never closed