multiple bug fixes and re-writes