Various functions added. Ugly hacks remaining.