added authentication with a bit of cleanup