almost all functions added, needs debugging