Refactor and fixes

https://github.com/rapid7/metasploit-framework/pull/2270