Cleaned up, documented, added checks