Improve and cleanup

This is a direct adaptation of my work on authenticate-ldap, please review: 
https://github.com/mlitchard/authenticate-ldap/pull/4