Re-write and added documentation