Fixed major bugs and added documentation