Environment-handling commands and some other changes

Added commands to define/remove environments, some other bug fixes and small improvements.