Better tests + code cleanup