Some code cleanup; added some additional tests