Implements ticket:3 and fixes ticket:42. The common functionality of