More code streamlining and general bug fixes. #324