Patch 1 continued

Continuing @ashtuchkin's work.