Big update with cleaned up code