Major refactor to make it compatible with ST3