Removed a lot of code duplication (moved to our gems).