Add tests and better environment variable handling