Added majority of rest of code from PureBasic.