some refactoring
mucho cleaning still needed before committing to apache repo