Added x_forwarded support and some bug fixing