optimisation and bugfixing

put most of the code into for loops and lists
made functions of most used parts
added more fallbacks