changed code, now much cleaner and added better support for reading materials