fixed more logical errors and added output which facilitates debugging